import java.util.*;

public class Darr {
    private Object [] data = null;
    private int length = 0;

    public Darr() {}

    public Darr(Object ... inputData) {
        for (Object n : inputData) {
            this.append(n);
        }
    }

    public int len() {
        return length;
    }

    //对象赋值或传参 传引用或赋值引用
    //要求调用者传入对象必须是独立实例(空间)
    public Darr append(Object ... inputData) {
/*
 *         if (inputData == null)
 *             return this;
 *
 *         Object [] newData = new Object[length + 1];
 *         if (data != null)
 *             System.arraycopy(data, 0, newData, 0, length);
 *         newData[length++] = inputData;
 *         data = newData;
 *         return this;
 */
        return insert(length + 1, inputData);
    }

    public Darr prepend(Object ... inputData) {
/*
 *         if (inputData == null)
 *             return this;
 *
 *         Object [] newData = new Object[length + 1];
 *         newData[0] = inputData;
 *         if (data != null) {
 *             System.arraycopy(data, 0, newData, 1, length);
 *         }
 *         length++;
 *         data = newData;
 *         return this;
 */
        return insert(1, inputData);
    }

    //指定位置插入数据, index标识第几个(从1开始计算)
    public Darr insert(int index, Object ... inputData) {
        if (inputData == null)
            return this;

        // System.out.printf("inputData len = %d, ", inputData.length);
        // System.out.println(Arrays.toString(inputData));

        if (index < 1)
            index = 1;
        else if (index > length)
            index = length + 1;

        Object [] newData = new Object[length + inputData.length];
        if (data != null) {
            System.arraycopy(data, 0, newData, 0, index - 1);
            // newData[index - 1] = inputData;
            System.arraycopy(inputData, 0, newData, index - 1, inputData.length);

            System.arraycopy(data, index - 1,
                                newData, index + inputData.length - 1,
                                data.length - index + 1);
        } else {
            System.arraycopy(inputData, 0, newData, 0, inputData.length);
        }
        length += inputData.length;
        data = newData;
        return this;
    }

    public Object indexOf(int index) {
        return data[index];
    }

    public Object [] toArray() {
        return Arrays.copyOf(this.data, this.length);
    }

    /*
     * sort         排序
     * find         查找
     * delete       删除
     * compare      比较
     */

    public Darr travel(DarrTravel t, Object Args) {
        for (Object n : this.data) {
            if (t == null)
                System.out.println(n);
            else
                t.objOp(n, Args);
        }
        return this;
    }

}
